# Maintainer: Diego Sogari <diego.sogari@gmail.com>
# Contributor: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=binaryen
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=123
pkgrel=2
pkgdesc="Compiler infrastructure and toolchain library for WebAssembly, in C++ (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/WebAssembly/binaryen"
msys2_references=(
  "cpe: cpe:/a:webassembly:binaryen"
)
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/WebAssembly/binaryen/archive/version_${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('a1e1caf250cab3a83938713594e55b6762591208e82087e3337f793e8c8eb7ab')

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DENABLE_WERROR=OFF \
    -DBUILD_TESTS=OFF \
    ${extra_config} \
    "../${_realname}-version_${pkgver}"

  cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" cmake --install .

  install -Dm644 "${srcdir}"/${_realname}-version_${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
